import type {
  Device,
  DeviceProperty,
  deviceTopic,
  ExampleReport,
  ThingModel,
} from './model';

import type { ID, PageQuery } from '#/api/common';
import type { Page } from '#/interface/page';

import { commonExport } from '#/api/helper';
import { requestClient } from '#/api/request';

enum Api {
  deptNodeInfo = '/system/dept/list/exclude',
  deviceAdd = '/iot/device/add',
  deviceExport = '',
  deviceInfo = '/iot/device/detail',
  deviceLatest = '/iot/device/property/latest',
  deviceProperty = '/iot/device/property/history',
  deviceRemove = '/iot/device/remove',
  devicetGroupList = '/iot/device/pageByGroup',
  devicetList = '/iot/device/page',
  deviceTopic = '/iot/device/topic/page',
  deviceUpdate = '/iot/device/update',
  exampleReport = '/iot/product/tslExample',
  mqttDeviceInfo = '/iot/device/linkInfos',
  root = '/system/dept',
  thingModelSelect = '/iot/thingModel/block/select',
}

/**
 * 设备属性记录列表
 * @returns list
 */
export function deviceTopic(params: PageQuery) {
  return requestClient.get<Page<deviceTopic>>(Api.deviceTopic, { params });
}

/**
 * 上报示例
 * @returns list
 */
export function exampleReport(params?: {
  deviceSn: string;
  productKey: string;
}) {
  return requestClient.get<ExampleReport>(Api.exampleReport, { params });
}

/**
 * mqtt连接参数
 * @returns list
 */
export function mqttDeviceInfo(params?: { deviceId: string }) {
  return requestClient.get<Page<Device>>(Api.mqttDeviceInfo, { params });
}

export function deviceProperty(params?: { pageNum: number; pageSize: number }) {
  return requestClient.get<Page<Device>>(Api.deviceProperty, { params });
}

export function devicetList(params?: { pageNum: number; pageSize: number }) {
  return requestClient.get<Page<Device>>(Api.devicetList, { params });
}

export function devicetGroupList(params?: {
  deptName?: string;
  status?: string;
}) {
  return requestClient.get<Page<Device>>(Api.devicetGroupList, { params });
}

/**
 * 查询部门列表（排除节点）
 * @param deptId 部门ID
 * @returns void
 */
export function deptNodeList(deptId: ID) {
  return requestClient.get<Device[]>(`${Api.deptNodeInfo}/${deptId}`);
}

/**
 * 部门详情
 * @returns 部门信息
 */
export function deviceInfo(params: { id: number | string }) {
  return requestClient.get<Device>(Api.deviceInfo, { params });
}

/**
 * 部门新增
 * @param data 参数
 */
export function deviceAdd(data: Partial<Device>) {
  return requestClient.postWithMsg<void>(Api.deviceAdd, data);
}

/**
 * 部门更新
 * @param data 参数
 */
export function deptUpdate(data: Partial<Device>) {
  return requestClient.postWithMsg<void>(Api.deviceUpdate, data);
}

/**
 * 注意这里只允许单删除
 * @param  id
 * @returns void
 */
export function deviceRemove(id: ID) {
  return requestClient.postWithMsg<void>(`${Api.deviceRemove}?id=${id}`);
}

/**
 * 导出excel
 * @param data data
 * @returns blob
 */
export function deviceExport(data: Partial<Device>) {
  return commonExport(Api.deviceExport, data);
}

export function thingModelSelect(params?: {
  blockName?: string;
  productId: string;
}) {
  return requestClient.get<ThingModel[]>(Api.thingModelSelect, { params });
}
/**
 * 设备属性记录列表
 * @returns list
 */
export function deviceLatest(params?: { blockId?: string; deviceId?: string }) {
  return requestClient.get<DeviceProperty[]>(Api.deviceLatest, { params });
}
